package com.wangzaiplus.test.juc.thread.ThreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ThreadPoolDemo1
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/30 10:13
 * @Version V1.0
 **/
public class ThreadPoolDemo1 {
    //ArrayBlockingQueue：是一个基于数组结构的有界阻塞队列，此队列按照先进先出原则对元素进行排序
    static ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(3,//核心线程数
            6,
            10,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(10),//工作队列
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
    );
    public static void main(String[] args){
        for (int i = 0; i <=17; i++) {
           int j=i;
           String taskName="任务"+j;
            threadPoolExecutor.execute(()->{
                //模拟任务内部处理耗时
                try {
                    TimeUnit.SECONDS.sleep(j);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + taskName + "处理完毕");
            });
        }
        threadPoolExecutor.shutdown();
    }
}
